package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetPostBasicDataBean;
import com.rjwh.dingdong.client.bean.localbean.PostBasicData;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.CommunityPostDetailModel;
import com.rjwh.dingdong.client.mvp.presenter.ICommunityPostDetailPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostDetailView;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.ToastUtil;
import com.rjwh.dingdong.client.util.UrlUtil;

import java.util.Set;

/**
 * @author wby
 * @ClassName CommunityPostDetailPresenter
 * @Description
 * @date 2016/6/1
 * @history 2016/6/1 author: description:
 */
public class CommunityPostDetailPresenter extends BasePresenter<CommunityPostDetailView> implements ICommunityPostDetailPresenter {
    private static final int REQUESTCODE = 321;

    private CommunityPostDetailModel model;

    public CommunityPostDetailPresenter(Context context, CommunityPostDetailView mMvpView) {
        super(context, mMvpView);
        model = new CommunityPostDetailModel(this);
    }


    @Override
    public void doGetPostBasicData(String topicid) {
        getMvpView().hideErrorView();
        model.doGetPostBasicData(topicid);
    }

    /**
     * 收藏
     *
     * @param topicid
     * @param operateid
     */
    public void doCollection(String topicid, String operateid) {
        model.doCollection(topicid, operateid);
    }

    /**
     * 点赞
     */
    public void doPraise(String topicid, String operateid) {
        model.doPraise(topicid, operateid);
    }


    /**
     * url拦截
     *
     * @param url
     * @return boolean
     */
    public boolean shouldOverrideUrl(String url) {
        boolean isLogined = MyApplication.spUtil.getBoolPreferenceByParamName(getContext(), LocalConstant.SP_IS_LOGINED);
        LogUtil.e("CommunityPostDetailActivity", "url: " + url);
        if (url.startsWith("http:") || url.startsWith("https:")) {
            if (isViewAttached()) {
                getMvpView().loadUrl(url);
            }
            return false;
        } else if (url.contains("tiandilixin")) {
            Uri uri = Uri.parse(url);
            Set<String> queryParameterNames = uri.getQueryParameterNames();
            if (queryParameterNames != null && !queryParameterNames.isEmpty() && queryParameterNames.contains(LocalConstant.ACTIONURL_TYPE)) {
                String type = uri.getQueryParameter(LocalConstant.ACTIONURL_TYPE);
                if(type.equals(LocalConstant.ACTIONURL_COMMUNITY)){
                    //type为community的情况处理帖子相关操作
                    String subtype = uri.getQueryParameter(LocalConstant.ACTIONURL_SUBTYPE);
                    if(subtype.equals(LocalConstant.ACTIONURL_REPLY)){
                        //subtype为reply时处理回复
                        Intent intent = UrlUtil.handelUrl(getContext(), url, false);
                        ((Activity) getContext()).startActivityForResult(intent, REQUESTCODE);
                    }else {
                        //其他subtype直接使用actionUrl处理跳转
                        Intent intent = UrlUtil.handelUrl(getContext(), url, true);
                    }
                }else {
                    //其他type(如Activity,跳转到webView)直接使用actionUrl处理跳转
                    UrlUtil.handelUrl(getContext(),url,true);
                }
                return true;
            }
        } else {
            return true;
        }
        return true;
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.COMMUNITY_ADDTOFAVORITEGOOD:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        // 调用js的方法改变点赞人的显示
                        if (getMvpView().isDoParise()) {
                            if (getMvpView().getPraiseCheck()) {// 点赞
                                String url = "javascript:dingdong.like('" + MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_USERID) + "','" + MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_USERNAME) + "')";
                                getMvpView().loadUrl(url);
                                getMvpView().setZanBtnChecked(true);
                                ToastUtil.showToast(getContext(), "已点赞");
                            } else {// 取消点赞
                                getMvpView().loadUrl("javascript:dingdong.unlike('" + MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_USERID) + "')");
                                getMvpView().setZanBtnChecked(false);
                                ToastUtil.showToast(getContext(), "取消点赞");
                            }
                        } else {
                            if (getMvpView().getFavorCheck()) {//收藏
                                getMvpView().setFavourBtnCheck(true);
                                ToastUtil.showToast(getContext(), "已收藏");
                            } else {//取消收藏
                                getMvpView().setFavourBtnCheck(false);
                                ToastUtil.showToast(getContext(), "取消收藏");
                            }
                        }
                    } else {
                        ToastUtil.showToast(getContext(), "操作失败,请稍后再试");
                    }
                    break;
                case NetConstant.COMMUNITY_GETPOSTBASICDATA:
                    ResGetPostBasicDataBean postBasicDataBean = (ResGetPostBasicDataBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && postBasicDataBean != null) {
                        getMvpView().hideErrorView();
                        PostBasicData postBasicData = postBasicDataBean.getData();
                        if (postBasicData != null) {
                            getMvpView().showMenuItems();
                            getMvpView().initPraiseViewState(postBasicData.getIsfavorited(), postBasicData.getIspraised());
                            getMvpView().loadShareData(postBasicData.getTitle(), postBasicData.getIntroduction(), postBasicData.getSharepath(), postBasicData.getImage());
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;

                default:
                    break;
            }
        }

    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
